package com.youngwang.threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程
 * 线程池为无限大，当执行第二个任务时第一个任务已经完成，会复用执行第一个任务的线程，而不用每次新建线程。
 * @author youngwang
 *         Date: 2016/8/31
 */
public class CacheThreadPool {

    public static void main(String[] args) {
        // 创建一个可缓存的线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executorService.execute(worker);
        }
        if(!executorService.isShutdown()){
            executorService.shutdown();
        }
        while (!executorService.isTerminated()){

        }
        System.out.println("Finished All Threads.");
    }
}
